/*
 * 
 * 
 * 
 */
package com.hboxs.asl.plugin.file;

import com.hboxs.asl.FileInfo.FileType;
import com.hboxs.asl.FileInfo.OrderType;
import com.hboxs.asl.Message;
import com.hboxs.asl.controller.admin.BaseController;
import com.hboxs.asl.entity.PluginConfig;
import com.hboxs.asl.service.FileService;
import com.hboxs.asl.service.PluginConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Controller - 本地文件存储
 */
@Controller("adminPluginFileController")
@RequestMapping("/asl-admin/storage_plugin/file")
public class FileController extends BaseController {

    @Resource(name = "filePlugin")
    private FilePlugin filePlugin;
    @Resource(name = "pluginConfigServiceImpl")
    private PluginConfigService pluginConfigService;
    @Resource(name = "fileServiceImpl")
    private FileService fileService;

    /**
     * 设置
     */
    @RequestMapping(value = "/setting", method = RequestMethod.GET)
    public String setting(ModelMap model) {
        PluginConfig pluginConfig = filePlugin.getPluginConfig();
        model.addAttribute("pluginConfig", pluginConfig);
        return "/plugin/file/setting";
    }

    /**
     * 更新
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public String update(Integer order, RedirectAttributes redirectAttributes) {
        PluginConfig pluginConfig = filePlugin.getPluginConfig();
        pluginConfig.setIsEnabled(true);
        pluginConfig.setOrder(order);
        pluginConfigService.update(pluginConfig);
        addFlashMessage(redirectAttributes, SUCCESS_MESSAGE);
        return "redirect:/asl-admin/storage_plugin/list.htm";
    }

    /**
     * 图片list
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(ModelMap model, String path) {
        if (path == null) {
            path = "/";
        }
        model.addAttribute("files", fileService.browser(path, FileType.image, OrderType.name));
        return "/admin/storage_plugin/file/list";
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public
    @ResponseBody
    Message del(String filename, HttpServletResponse response, HttpServletRequest request) {
        boolean res = fileService.del(FileType.image, filename);
        if (res == Boolean.TRUE) {
            return SUCCESS_MESSAGE;
        }
        return ERROR_MESSAGE;
    }

}